package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ProductTest;
import com.ruoyi.system.domain.vo.ProductTestExportVo;
import com.ruoyi.system.domain.vo.ProductTestImportVo;
import com.ruoyi.system.listener.ProductTestImportListener;
import com.ruoyi.system.service.IProductTestService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 产品test控制器
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/productTest")
public class ProductTestController extends BaseController {

    private final IProductTestService productTestService;

    /**
     * 查询产品test列表
     */
    @SaCheckPermission("system:productTest:list")
    @GetMapping("/list")
    public TableDataInfo<ProductTest> list(ProductTest productTest, PageQuery pageQuery) {
        return productTestService.selectPageProductTestList(productTest, pageQuery);
    }

    /**
     * 导出产品test列表
     */
    @Log(title = "产品test管理", businessType = BusinessType.EXPORT)
    @SaCheckPermission("system:productTest:export")
    @PostMapping("/export")
    public void export(ProductTest productTest, HttpServletResponse response) {
        List<ProductTest> list = productTestService.selectProductTestList(productTest);
        List<ProductTestExportVo> listVo = BeanUtil.copyToList(list, ProductTestExportVo.class);
        ExcelUtil.exportExcel(listVo, "产品test数据", ProductTestExportVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "产品test管理", businessType = BusinessType.IMPORT)
    @SaCheckPermission("system:productTest:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<ProductTestImportVo> result = ExcelUtil.importExcel(file.getInputStream(), ProductTestImportVo.class, new ProductTestImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "产品test数据", ProductTestImportVo.class, response);
    }

    /**
     * 获取产品test详细信息
     *
     * @param id 产品testID
     */
    @SaCheckPermission("system:productTest:query")
    @GetMapping(value = "/{id}")
    public R<ProductTest> getInfo(@PathVariable("id") Long id) {
        return R.ok(productTestService.selectProductTestById(id));
    }

    /**
     * 新增产品test
     */
    @SaCheckPermission("system:productTest:add")
    @Log(title = "产品test管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody ProductTest productTest) {
        return toAjax(productTestService.insertProductTest(productTest));
    }

    /**
     * 修改产品test
     */
    @SaCheckPermission("system:productTest:edit")
    @Log(title = "产品test管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody ProductTest productTest) {
        return toAjax(productTestService.updateProductTest(productTest));
    }

    /**
     * 删除产品test
     *
     * @param ids 产品testID串
     */
    @SaCheckPermission("system:productTest:remove")
    @Log(title = "产品test管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(productTestService.deleteProductTestByIds( Arrays.asList(ids)));
    }
}
